Eddie's Lessons, version 10

Customizing the Syntax Colors

level: any

by Alyce Watson [http://alycesrestaurant.com/]

Home

Tip Corner: ByRef

API Corner: File Download

Working with Strings 3

Stylebits Corner: Dialogs

Eddie's Lessons, v.10

Liberty Basic Wiki

Preprocessor 1

Preprocessor 2

Find Folder

Multiple Listboxes

Newsletter help

Index


What is Eddie?

Eddie is a new code editor written in Liberty BASIC, for Liberty BASIC. Eddie can be customized and extended to meet the needs of the individual programmer. The newest version is available here: Eddie!


Syntax Colors

The previous installment of Eddie's Lessons discussed syntax coloring and why it is used. By default, CodeAChrome uses syntax coloring of some standard programming keywords. We've already given the user the choice to turn syntax coloring on or off, and Eddie has replaced the default CodeAChrome keywords with Liberty BASIC's keywords.

This lesson explains how to customize the colors used. The default keyword color in CodeAChrome is blue. Quoted strings are purple (darkpink in LB terminology). Comments are green. Operators are red. Operators are symbols like + - ( ) [ ] < > = / * All other text is black by default, and the background is white.

RED GREEN BLUE

Colors are made up of red, green, and blue components. Each of these must be in the range of 0 - 255, with 0 being none of a color and 255 being complete saturation of that color. True red has a value of 255 for red, 0 for green and 0 for blue. White has a value of 255 for all three components. Black has a value of 0 for all three components. [editors note: Check out the Tip Corner which has a RGB conversion function]

CodeAChrome allows you to set values for red, green, and blue. Each value must be in the range of 0 - 255. If you'd like to experiment with RGB values to see how changes affect the colors produced, have a look at this tiny demo. It fills a graphics window with yellow, which is RGB "255 255 0". Change those values to create different colors, then run it again to see the result.

nomainwin
open "RGB" for graphics as #1
#1 "trapclose Quit"
#1 "down"
#1 "fill 255 255 0"
#1 "flush"
wait

sub Quit handle$
close #handle$
end
end sub

Changing the Colors

CodeAChrome allows you to change colors with these functions:

All of the functions work in exactly the same way, so we'll discuss just one of them in detail. The example uses SetKeyColor , which sets the color used to display keywords. The method is the same for all functions to set colors.

Changing the Keyword Color

The function that changes the keyword color is SetKeyColor and it looks like this:

    calldll #r, "SetKeyColor",_ 'set color for keywords
        red as long,_     'red 0-255
        green as long,_   'green 0-255
        blue as long,_    'blue 0-255
        re as void

To set the keyword color to pure red, use this code:

    calldll #r, "SetKeyColor",_ 'set color for keywords
        255 as long,_     'red 0-255
        0 as long,_       'green 0-255
        0 as long,_       'blue 0-255
        re as void

To create a purple color (Liberty BASIC color "pink") use this code:

    calldll #r, "SetKeyColor",_ 'set color for keywords
        255 as long,_     'red 0-255
        0 as long,_       'green 0-255
        255 as long,_     'blue 0-255
        re as void

This example creates orange keywords.

    calldll #r, "SetKeyColor",_ 'set color for keywords
        243 as long,_   'red 0-255
        172 as long,_   'green 0-255
        39 as long,_    'blue 0-255
        re as void

Use exactly the same method for all of the color functions.

Refreshing the Syntax Coloring

CodeAChrome provides a function that causes the syntax coloring to be refreshed. Use it after any of the functions that cause color changes.

    calldll #r, "ColorAll",_    'recolor text to reflect user choices
        re as void

Eddie Lets the User Choose the Colors

For each color choice, Eddie includes a SUB. The SUB accepts a string that contains the red, green and blue values for the specified color. The color string required by the sub would look like this for pure red:

"255 0 0"

The string to produce orange would look like this:

"243 172 39"

The string is then broken into separate red, green and blue values with the WORD$() function. The first word is the red value, the second word is the green value, and the third word is the blue value. Since WORD$() returns a string, we use the VAL() function to translate them to numeric values, as required by the API function. Here is the SUB to change the keyword color. The SUBS to change the other colors work in exactly the same way.

Sub SetKeywordColor Color$ 
    red = val(word$(Color$,1))
    green = val(word$(Color$,2))
    blue = val(word$(Color$,3))
    calldll #r, "SetKeyColor",_ 'set color for keywords
        red as long,_     'red 0-255
        green as long,_   'green 0-255
        blue as long,_    'blue 0-255
        re as void
    calldll #r, "ColorAll",_    'recolor text to reflect user choices
        re as void
    End Sub

Getting the User's Color Selection

Eddie adds items to the Options Menu for color selections.

Menu        #1, "&Options", "&Syntax Color On/Off",[syntaxColor],"&Line Numbers On/Off",[lineNumbers],|,_
            "Set &Default Color", [doDefaultColor],"Set &Background Color", [doBackgroundColor],|,_
            "Set &Keyword Color", [doKeywordColor],"Set &Comment Color", [doCommentColor],"Set &Quote Color",_
            [doQuoteColor],"Set &Operator Color", [doOperatorColor]

Here is the routine invoked by the "Set Keyword Color" menu item:

[doKeywordColor]
    colordialog KeywordColor$, KeywordColor$
    call SetKeywordColor KeywordColor$
    wait

It calls up the Liberty BASIC COLORDIALOG function to get the user's color choice, which it places into the variable called KeywordColor$ . It then calls the SUB to set the keyword color. The colordialog returns a string with the user's color choice. If the user selected orange, the KeywordColor$ variable would look like this:

"243 172 39"

If the user selected red, the variable would hold the following information:

"255 0 0 red"

Since pure red is one of the named Liberty BASIC colors, that word "red" is the fourth word in the string. Since Eddie has no need of that information, the optional fourth word in the string returned by COLORDIALOG is ignored.

The COLORDIALOG is seeded with a string, and that color is highlighted when the dialog opens. Eddie keeps the values for each of the colors in a special variable. These variables are initialized at the start of the program:

    TextColor$ = "0 0 0"            'default text color = black
    BackColor$ = "255 255 255"      'default background color = white
    KeywordColor$ = "0 0 255"       'default keyword color = blue
    QuoteColor$ = "123 0 123"       'default quote color = darkpink
    CommentColor$ = "0 123 0"       'default comment color = darkgreen
    OperatorColor$ = "255 0 0"      'default operator color = red

As soon as the program window opens, the colors are set, by calling on the SUBS, as follows:

    call SetTextColor TextColor$
    call SetBackgroundColor BackColor$
    call SetKeywordColor KeywordColor$
    call SetQuoteColor QuoteColor$
    call SetCommentColor CommentColor$
    call SetOperatorColor OperatorColor$

Saving User's Color Choices

Eddie doesn't want to make the user select his favorite colors each time the program is run, so Eddie saves these choices to the INI file.

[writeIniFile]
    open IniFile$ for output as #f
    print #f, LibertyExe$
    print #f, Freeform$
    print #f, Runtime$
    print #f, Help$
    print #f, TextColor$ 
    print #f, BackColor$ 
    print #f, KeywordColor$ 
    print #f, QuoteColor$ 
    print #f, CommentColor$ 
    print #f, OperatorColor$ 
    close #f
    RETURN

The INI file is read before the window is opened, so the user's color choices are loaded into those special variables, and the colors are set automatically at the beginning of the program.

[readIniFile]
    open IniFile$ for append as #f
    lenFile = LOF(#f)
    close #f

    'if the length = 0, the file didn't exist, so get info from user
    if lenFile=0 then
        notice "To use Eddie successfully, you must set the path to Liberty.exe, Freeform, the runtime engine and the helpfile."
        gosub [GetLibertyPath]
        gosub [GetFreeformPath]
        gosub [GetRuntimePath]
        gosub [GetHelpPath]
    else
        'if the length is greater than 0, the file exists
        'open it for input and read the first line into LibertyExe$
        open IniFile$ for input as #f
        line input #f, LibertyExe$
        'read second line into Freeform$, only if end of file not reached
        if eof(#f) = 0 then line input #f, Freeform$
        'read third line into Runtime$, only if end of file not reached
        if eof(#f) = 0 then line input #f, Runtime$
        'read fourth line into Help$, only if end of file not reached
        if eof(#f) = 0 then line input #f, Help$

        if eof(#f) = 0 then line input #f,TextColor$ 'default text color
        if eof(#f) = 0 then line input #f,BackColor$ 'default background color
        if eof(#f) = 0 then line input #f,KeywordColor$ 'default keyword color
        if eof(#f) = 0 then line input #f,QuoteColor$ 'default quote color
        if eof(#f) = 0 then line input #f,CommentColor$ 'default comment color
        if eof(#f) = 0 then line input #f,OperatorColor$ 'default operator color
        close #f
    end if
    RETURN

There's More!

Watch for more features of CodeAChrome in future versions of Eddie.


Home

Tip Corner: ByRef

API Corner: File Download

Working with Strings 3

Stylebits Corner: Dialogs

Eddie's Lessons, v.10

Liberty Basic Wiki

Preprocessor 1

Preprocessor 2

Find Folder

Multiple Listboxes

Newsletter help

Index